package com.luca.quartz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luca.common.utils.StringUtils;
import com.luca.quartz.domain.SysJobLog;
import com.luca.quartz.mapper.SysJobLogMapper;
import com.luca.quartz.service.ISysJobLogService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 定时任务调度日志信息 服务层
 *
 * @author luca
 */
@Service
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog> implements ISysJobLogService {

    /**
     * 获取quartz调度器日志的计划任务
     *
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public List<SysJobLog> selectJobLogList(SysJobLog jobLog) {
        var query = new QueryWrapper<SysJobLog>();
        if (StringUtils.isNotEmpty(jobLog.getJobName())) {
            query.like("job_name", jobLog.getJobName());
        }
        if (StringUtils.isNotEmpty(jobLog.getJobGroup())) {
            query.eq("job_group", jobLog.getJobGroup());
        }
        if (StringUtils.isNotEmpty(jobLog.getStatus())) {
            query.eq("status", jobLog.getStatus());
        }
        if (StringUtils.isNotEmpty(jobLog.getInvokeTarget())) {
            query.like("invoke_target", jobLog.getInvokeTarget());
        }
        if (!ObjectUtils.isEmpty(jobLog.getParams().get("beginTime"))) {
            query.ge("create_time", jobLog.getParams().get("beginTime"));
        }
        if (!ObjectUtils.isEmpty(jobLog.getParams().get("endTime"))) {
            query.le("create_time", jobLog.getParams().get("endTime"));
        }
        return baseMapper.selectList(query);
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(Long jobLogId) {
        return baseMapper.selectById(jobLogId);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog) {
        baseMapper.insert(jobLog);
    }

    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(Long[] logIds) {
        return baseMapper.deleteBatchIds(Arrays.asList(logIds));
    }

    /**
     * 删除任务日志
     *
     * @param jobId 调度日志ID
     */
    @Override
    public int deleteJobLogById(Long jobId) {
        return baseMapper.deleteById(jobId);
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog() {
        baseMapper.cleanJobLog();
    }
}
